package com.library.repository.impl;

import com.library.entity.Admin;
import com.library.entity.Reader;
import com.library.repository.AdminRepository;
import com.library.repository.ReaderRepository;
import com.library.utils.JDBCTools;
import javafx.beans.binding.ObjectExpression;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdminRepositoryImpl implements AdminRepository {

    @Override
    public Object login(String username, String password) {
        Connection connection = JDBCTools.getConnection();
        String sql = "select * from bookadmin where username = ? and password = ?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Admin admin = null;
        try {
            statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, password);
            resultSet = statement.executeQuery();
            while(resultSet.next()){
                admin = new Admin(resultSet.getInt(1),resultSet.getString(2),resultSet.getString(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCTools.release(connection,statement,resultSet);
        }
        return admin;
    }

    /**
     * 处理图书借阅信息
     * @param borrowId
     * @param state
     * @param adminId
     */
    @Override
    public void handle(Integer borrowId, Integer state, Integer adminId) {
        Connection conn = JDBCTools.getConnection();
        PreparedStatement statement = null;
        String sql = "update borrow set state = ?,adminid = ? where borrow.id = ?";
        try {
            statement = conn.prepareStatement(sql);
            statement.setInt(1, state);
            statement.setInt(2, adminId);
            statement.setInt(3,borrowId);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(conn,statement,null);
        }
    }
}
